package com.pinganfu.mock.control;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pinganfu.mock.model.MethodEntity;
import com.pinganfu.mock.model.TrendsEntity;
import com.pinganfu.mock.service.IMethodService;
import com.pinganfu.mock.service.ITrendsService;


@Controller
@RequestMapping(value = "/Trends", produces = { "application/json;charset=UTF-8"})
public class TrendsControl {
	@Resource
	private ITrendsService iTrendsService;
	
	@Resource
	private IMethodService iMethodService;
	
	@RequestMapping(value="/getTrends",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String getTrends(@RequestParam String name){

		TrendsEntity entity = new TrendsEntity();
		entity.setName(name);
		List<TrendsEntity> entities = iTrendsService.getTrends(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("trendsList", entities);
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/getTrendsList",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String getTrendsList(){
		
		List<String> entities = iTrendsService.getTrendsList();
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("trendsNameList", entities);
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/getTrendsValue",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String getTrendsValue(@RequestParam String name){

		TrendsEntity entity = new TrendsEntity();
		entity.setName(name);
		
		String entities = iTrendsService.getTrendsValue(name);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("trendsNameList", entities);
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/insertTrends",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String insertTrends(
			@RequestParam String name,
			@RequestParam String value,
			@RequestParam String description
			){
		TrendsEntity entity = new TrendsEntity();
		entity.setName(name);
		entity.setDescription(description);
		entity.setValue(value);
		entity.setCreateTime(new Date());
		
		Integer id = iTrendsService.insertTrends(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("insertTrends", id);
   		return json.messageToJsonString();
	}
	
	
	@RequestMapping(value="/updateTrends",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String updateTrends(
			@RequestParam Integer id,
			@RequestParam String name,
			@RequestParam String value,
			@RequestParam String description
			){
		TrendsEntity entity = new TrendsEntity();
		if (null != id)
			entity.setId(id);
		if (null != name)
			entity.setName(name);
		if (null != description)
			entity.setDescription(description);
		if (null != value)
			entity.setValue(value);
		
		iTrendsService.updateTrends(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("updateTrends", entity.getId());
   		return json.messageToJsonString();
	}
	
	@RequestMapping(value="/delTrends",method = { RequestMethod.GET, RequestMethod.POST })
	public @ResponseBody String delTrends(
			@RequestParam String name
			){
		TrendsEntity entity = new TrendsEntity();
		entity.setName(name);
		
		iTrendsService.delTrends(entity);
		
		AbsJsonObject json = new AbsJsonObject(AbsJsonObject.SUCCESS_OK);
   		json.addMessage("delTrends", entity.getId());
   		return json.messageToJsonString();
	}
}
